package com.vote.vs.controller;

import com.vote.vs.entity.User;
import com.vote.vs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/login")
    public Object login(String username, String password) {
        Boolean aBoolean = userService.login(username, password);
        HashMap<String, String> map = new HashMap<>();
        System.out.println(username + password);
        if (aBoolean) {
            map.put("msg", "1");
        } else {
            map.put("msg", "登录失败");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/regist")
    public Object regist(User user, String confirmPassword) {
        System.out.println("--------------------" + user);
        System.out.println("--------------------" + confirmPassword);
        HashMap<String, String> map = new HashMap<>();
        if (user.getVuPassword().equals(confirmPassword)) {
            Boolean aBoolean = userService.regist(user);
            if (aBoolean) {
                map.put("msg", "1");
            } else {
                map.put("msg", "用户名已存在");
            }
        } else {
            map.put("msg", "密码输入不一致，请重新注册");
        }

        return map;

    }
}
